package top.mcwebsite.playandroid.common.anim

import androidx.compose.animation.core.FastOutSlowInEasing
import androidx.compose.animation.core.InfiniteRepeatableSpec
import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.RepeatMode
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.rememberInfiniteTransition
import androidx.compose.animation.core.tween
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp

@Preview
@Composable
fun TwoArcLoading(modifier: Modifier = Modifier.size(200.dp)) {
    BoxWithConstraints {
        val (widthPx, heightPx) = with(LocalDensity.current) {
            maxWidth.toPx() to maxHeight.toPx()
        }
        val size = minOf(widthPx, heightPx)
        val centerX = widthPx / 2
        val centerY = heightPx / 2
        val radius = size / 4
        val transition = rememberInfiniteTransition()
        val angleDiff = transition.animateFloat(
            initialValue = 0f,
            targetValue = 360f,
            animationSpec = InfiniteRepeatableSpec(
                tween(
                    durationMillis = 1000,
                    easing = FastOutSlowInEasing
                )
            )
        )
        val circleSize = transition.animateFloat(
            initialValue = 20f,
            targetValue = 60f,
            animationSpec = InfiniteRepeatableSpec(
                tween(durationMillis = 1000, easing = LinearEasing),
                repeatMode = RepeatMode.Reverse
            )
        )

        val paintColor = MaterialTheme.colorScheme.onSurface
        Canvas(
            modifier = modifier
                .background(color = MaterialTheme.colorScheme.surface)
                .padding(10.dp)
        ) {
            drawArc(
                color = paintColor,
                startAngle = 0f + angleDiff.value,
                sweepAngle = 90f,
                useCenter = false,
                topLeft = Offset(centerX - radius, centerY - radius),
                size = Size(radius * 2, radius * 2),
                style = Stroke(10f, cap = StrokeCap.Round)
            )
            drawArc(
                color = paintColor,
                startAngle = 180f + angleDiff.value,
                sweepAngle = 90f,
                useCenter = false,
                topLeft = Offset(centerX - radius, centerY - radius),
                size = Size(radius * 2, radius * 2),
                style = Stroke(10f, cap = StrokeCap.Round)
            )
            drawCircle(
                color = paintColor,
                center = Offset(centerX, centerY),
                style = Stroke(10f),
                radius = circleSize.value
            )
        }
    }
}